<template>
    <div>
  <el-table
    :data="tableData" border style="width: 100%"><el-table-column prop="id" label="ID"></el-table-column>
    <el-table-column prop="name" label="商品名称"></el-table-column>
    <el-table-column prop="is_new" label="新品">
       <template v-slot="scope">
            {{scope.row.is_new?'是':'否'}}
    </template>
    </el-table-column>
  </el-table>
   <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-upload
  class="avatar-uploader"
  action=""
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload"
  :http-request="uploadGoodPic"
  >
  <img v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
    </div>
</template>

<script>
import {AdminGoodsApi,UploadGoodPicApi} from '@/request/api'
export default {
    data () {
        return {
            tableData:[],
            currentPage:1,
            pageSize:10,
            total:0,
            imageUrl:''
            
        }
    },
    methods:{
        uploadGoodPic(file){
            console.log(file);
            let formdata=new FormData();
            formdata.append('good_pic',data.file)
            UploadGoodPicApi(formdata).then((res)=>{
                console.log(res);
                 if(res.errno===0){
                this.imageUrl=res.data.fileUrl
            }
            })
        },
        handleAvatarSuccess(res, file){
            // console.log(res);
            if(res.errno===0){
                this.imageUrl=res.data.fileUrl
            }
        },
        //上传图片之前的钩子
        beforeAvatarUpload(file){},
        handleSizeChange(size){
            this.pageSize=size
            //页面加载,数量也会发生改变
           this.fatchData()
        },
        handleCurrentChange(page){
            // console.log("Page",page);
            this.currentPage=page
           this.fatchData()
        },
        fatchData(){
        AdminGoodsApi({
             page:this.currentPage,
             // name:'haha',
             size:this.pageSize,
       }).then((res)=>{
        if(res.errno===0){
            this.tableData=res.data.data
            this.total=res.data.count
        }
       })
        }
    },
   created(){
     this.fatchData()
   }
}
</script>
 
<style lang = "less" scoped>
    
</style>